package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.core.HttpEngineConduits;
import com.foolishbird.sdk.handler.SimpleAsyncResponseHandler;
import com.foolishbird.sdk.params.BindPhoneResponseParams;
import com.foolishbird.sdk.params.FloatKefuResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.SendVerifyResponseParams;
import com.foolishbird.sdk.params.bean.AccountInfo;
import com.foolishbird.sdk.params.bean.FloatUserInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.params.bean.VerifyType;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.ui.assist.OnPhoneVerifyCodeListener;
import com.foolishbird.sdk.ui.assist.VerifyCodeCountDownTimer;
import com.foolishbird.sdk.ui.view.DialogFactory;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.ToolsUtils;


public class FloatChangePwdPwdFragment extends BaseHttpFragment implements View.OnClickListener {


//    private LinearLayout phoneLayout,realnameLayout,versionLayout,pwdLayout;
//    private TextView titleTxt;
    private ImageView backView;
    EditText phoneText,codeText;
    Button confirmBtn;



    @Override
    public void onStop() {
        super.onStop();
    }




    public static final FloatChangePwdPwdFragment newInstance(Bundle bundle) {
        FloatChangePwdPwdFragment fragment = new FloatChangePwdPwdFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_float_changepwd_pwd");
        View contentView =inflater.inflate(resource,null);
//        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
//        titleTxt.setText("修改密码");
        backView = contentView.findViewById(getViewIdentifier("fb_float_close"));
        backView.setOnClickListener(this);
        phoneText = contentView.findViewById(getViewIdentifier("fb_fragment_phone"));
        codeText = contentView.findViewById(getViewIdentifier("fb_fragment_login_code_edit"));
        confirmBtn = contentView.findViewById(getViewIdentifier("fb_fragment_login_login_btn"));

        confirmBtn.setOnClickListener(this);

        return contentView;

    }

    private FloatUserInfo userInfo;

private int tastId;

    @Override
    public void onClick(View view) {
        if(view == backView){
//            if(userInfo!=null&&!TextUtils.isEmpty(userInfo.getId_number())){
//
//            }
            MyFragmentManger.getInstance().goBack(this);
        }else if(view == confirmBtn){
            String phoneNumber = phoneText.getText().toString().trim();
            String code = codeText.getText().toString().trim();
            if(TextUtils.isEmpty(phoneNumber)){
                Toast.makeText(getContext(),"请输入原密码",Toast.LENGTH_SHORT).show();
            }else if (TextUtils.isEmpty(code)){
                Toast.makeText(getContext(),"请输入新密码",Toast.LENGTH_SHORT).show();
            }else{
                SimpleAsyncResponseHandler handler = requestCallbackHandler(FloatKefuResponseParams.class,code);
                tastId =HttpEngineApi.getInstance().changePwdByPwd(phoneNumber, code, handler);
            }


        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if (flag ==tastId ) {

                    Toast.makeText(getContext(),"修改成功",Toast.LENGTH_SHORT).show();
                    HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
                    AccountInfo info = wanSDKDataDao.findUserAccountByUserId(wanSDKDataDao.readCurrentLoginUserUID());
                    if(info!=null){
                        info.setPassword(params!=null&&params.length>0?(String)params[0]:"");
                        wanSDKDataDao.saveUserAccount(info);
                    }
                    wanSDKDataDao.writeAutoLogin(false);
                    MyFragmentManger.getInstance().goBack(this);
                }
            }
        }
    }


}
